class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        if (nums.size() == 1)return 1;
        if (nums.size() == 0)return 0;
        sort(nums.begin(), nums.end());
        int cnt = 1;
        int n = nums.size();
        int j = 1;
        vector<int>ans(n);
        ans[0] = nums[0];
        for (int i = 1; i < n; i++) {
            if (nums[i] != nums[i - 1]) {
                ans[j++] = nums[i];
            }
        }
        int ma = 1;
        for (int i = 1; i < j; i++) {
            if (ans[i] == ans[i - 1] + 1) {
                cnt++;
            }
            else cnt = 1;
            ma = max(ma, cnt);
        }
        return ma;
    }
};